﻿using System;
using System.Collections;
using System.Data;
using System;
using System.Collections;
using System.Data;
using CE.Database;

/// <summary>
/// Summary description for TAPromotionDB
/// </summary>
/// 
namespace CE.Provider
{
    public class TAPromotionDB
    {
        public TAPromotionDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        
        public Int64 Insert(TAPromotion carpromotion)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "[sprocProviderTAPromotionInsertUpdateSingleItem]";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@PromotionId", carpromotion.PromotionId);
            _sPara.Add("@ProviderCompanyId", carpromotion.ProviderCompanyId);
            _sPara.Add("@PromotionTitle", carpromotion.PromotionTitle);
            _sPara.Add("@PromotionDescription", carpromotion.PromotionDescription);
            _sPara.Add("@RefCEUserId", carpromotion.RefCEUserId);
            _sPara.Add("@Status", carpromotion.Status);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public TAPromotionList GetProviderTAPromotionList(Int64 ProviderCompanyId)
        {
            TAPromotionList _TAPromotionlist = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocProviderTAPromotionSelectItemList";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCompanyId", ProviderCompanyId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _TAPromotionlist = new TAPromotionList();
                    for (int i = 0; i < _dt.Rows.Count; i++)
                    {
                        TAPromotion _TAPromotion = new TAPromotion();
                        _TAPromotion = FillDataRecord(_dt, i);
                        _TAPromotionlist.Add(_TAPromotion);
                    }
                }
            }
            return _TAPromotionlist;
        }

        public TAPromotion GetProviderTAPromotion(Int64 PromotionId)
        {
            TAPromotion _carpromotion = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocProviderTAPromotionSelectSingleItem";

            SortedList _spara = new SortedList();
            _spara.Add("@PromotionId", PromotionId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _carpromotion = new TAPromotion();
                    _carpromotion = FillDataRecord(_dt, 0);
                }
            }
            return _carpromotion;
        }


        private TAPromotion FillDataRecord(DataTable myDataTable, int RowId)
        {
            TAPromotion myTAPromotion = new TAPromotion();
            myTAPromotion.PromotionId = Convert.ToInt64(myDataTable.Rows[RowId]["PromotionId"].ToString());
            myTAPromotion.ProviderCompanyId = Convert.ToInt64(myDataTable.Rows[RowId]["ProviderCompanyId"].ToString());
            myTAPromotion.PromotionTitle = myDataTable.Rows[RowId]["PromotionTitle"].ToString();
            myTAPromotion.PromotionDescription = myDataTable.Rows[RowId]["PromotionDescription"].ToString();
            myTAPromotion.RefCEUserId = Convert.ToInt64(myDataTable.Rows[RowId]["RefCEUserId"].ToString());
            myTAPromotion.Status = Convert.ToBoolean(myDataTable.Rows[RowId]["Status"].ToString());
            myTAPromotion.ModifiedDate = DateTime.Parse(myDataTable.Rows[RowId]["ModifiedDate"].ToString());
            return myTAPromotion;
        }
    }
}